home *** CD-ROM | disk | FTP | other *** search
- Path: news.nevada.edu!not-for-mail
- From: kesslert@nevada.edu (Troy Kessler)
- Newsgroups: comp.lang.c++
- Subject: Need Help With CSC Homework
- Date: 16 Feb 1996 02:42:31 GMT
- Organization: University of Nevada System Computing Services
- Message-ID: <4g0qun$jp@news.nevada.edu>
- NNTP-Posting-Host: unauthenticated_user@pioneer.nevada.edu
- X-Newsreader: TIN [UNIX 1.3 950520BETA PL0]
-
- pfun.cpp
-
- //*************************************************
- //** ROMANFUN.CPP **
- //*************************************************
- //** This program will add, subtract, multiply, **
- //** and divide roman numbers. The numbers are **
- //** assumed to be less than 20 characters long. **
- //** The roman numbers should not be inputed in **
- //** subtraction form. Examples - IV=6 IIII=4 **
- //*************************************************
- #include <iostream.h> //for cin and cout
- #include "p.h" //has typedef and prototypes
- istream& operator>>(istream& in,Romantype& x)
- {
- in>>x.Roman;
- return in;
- }
-
- ostream& operator<<(ostream& out,Romantype x)
- {
- out<<x.Roman;
- return out;
- }
-
- Romantype::Romantype()
- {
- int i;
- for(i=1;i<MAXLENGTH;i++)
- {
- Roman[i]=' ';
- }
- }
-
- //*************************************************
- //** ADD FUNCTION **
- //*************************************************
- //** This function adds two roman numbers. It **
- //** requires a character array and two roman **
- //** numbers. **
- //*************************************************
- void Romantype::operator+(Romantype numbertwo)
- {
- int numbero;
- int numbert;
- int answer;
- Romantype answerr;
- numbero=RomanToDecimal(Roman);
- numbert=RomanToDecimal(numbertwo.Roman);
- answer=numbero+numbert;
- DecimalToRoman(answerr.Roman,answer);
- cout<<answerr.Roman;
- }
-
- //*************************************************
- //** ROMAN TO DECIMAL FUNCTION **
- //*************************************************
- //** This function converts a roman number to a **
- //** decimal number. It requires the character **
- //** array and the number of characters in the **
- //** array. **
- //*************************************************
- int Romantype::RomanToDecimal(char[MAXLENGTH])
- {
- int number=0; //value of number intialized to zero
- int i; //used in for loop
- for(i=1;i<MAXLENGTH;i++) //until end of array add values of letters
- {
- switch(Roman[i])
- {
- case 'I':number=number+1;break;
- case 'V':number=number+5;break;
- case 'X':number=number+10;break;
- case 'L':number=number+50;break;
- case 'C':number=number+100;break;
- case 'D':number=number+500;break;
- case 'M':number=number+1000;break;
- }
- }
- return number; //return decimal value to where the function was called
- }
-
- //*************************************************
- //** DECIMAL TO ROMAN FUNCTION **
- //*************************************************
- //** This function converts a decimal number to **
- //** a roman number. It requires a character **
- //** array and the value of the decimal number. **
- //*************************************************
- void Romantype::DecimalToRoman(char[MAXLENGTH],int answer)
- {
- int i=1; //used in for loop
- while(answer-1000>=0) //convert to roman by storing letters in array
- {
- answerr.Roman[i]='M';
- answer=answer-1000;
- i++;
- }
- while(answer-500>=0)
- {
- answerr.Roman[i]='D';
- answer=answer-500;
- i++;
- }
- while(answer-100>=0)
- {
- answerr.Roman[i]='C';
- answer=answer-100;
- i++;
- }
- while(answer-50>=0)
- {
- answerr.Roman[i]='L';
- answer=answer-50;
- i++;
- }
- while(answer-10>=0)
- {
- answerr.Roman[i]='X';
- answer=answer-10;
- i++;
- }
- while(answer-5>=0)
- {
- answerr.Roman[i]='V';
- answer=answer-5;
- i++;
- }
- while(answer-1>=0)
- {
- answerr.Roman[i]='I';
- answer=answer-1;
- i++;
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-
-